<!-- 登录弹出框 -->
<template>
  <div class="login-box__">
    <el-tabs v-model="loginBoxTabPage" @tab-click="loginInfo = {}">
      <el-tab-pane label="密码登录" name="pwdLogin">
        <div class="login-info">
          <el-form :model="loginInfo" label-width="60px">
            <el-form-item label="账号">
              <el-input
                placeholder="请输入手机号或邮箱"
                v-model="loginInfo.account"
                size="large"
              >
                <template slot="prepend">账号</template>
              </el-input>
            </el-form-item>
            <el-form-item label="密码">
              <el-input
                placeholder="请输入密码"
                v-model="loginInfo.password"
                size="large"
                type="password"
                show-password
              />
            </el-form-item>
          </el-form>
          <div>
            <el-button
              @click="
                loginBoxTabPage = 'codeLogin';
                loginInfo.password = '';
              "
              >注册</el-button
            >
            <el-button @click="login" type="primary">登录</el-button>
          </div>
          <i>欢迎登录</i>
        </div>
      </el-tab-pane>
      <el-tab-pane label="短信登录" name="codeLogin">
        <div class="login-info">
          <el-form :model="loginInfo" label-width="60px">
            <el-form-item label="账号">
              <el-input
                placeholder="请输入手机号或邮箱"
                v-model="loginInfo.account"
                size="large"
              >
                <template #append>
                  <el-button @click="sendCode">获取验证码</el-button>
                </template>
              </el-input>
            </el-form-item>
            <el-form-item label="验证码">
              <el-input
                placeholder="请输入验证码"
                v-model="loginInfo.code"
                maxlength="6"
                show-word-limit
                size="large"
              />
            </el-form-item>
          </el-form>
          <el-button @click="login" type="primary"> 登录 / 注册</el-button>
          <i>未注册过手机或邮箱，我们会自动帮您注册</i>
        </div>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script lang="ts" setup>
import { ref } from "vue";
import { errorNotify, isNull, setToken, successNotify } from "@/utils/common";
import userApi from "@/api/user";
import commonApi from "@/api/common";
import { LoginInfo } from "@/types/user";

// 登录信息
const loginInfo = ref<LoginInfo>({
  account: "19150932003",
});
// 登录方式
const loginBoxTabPage = ref("codeLogin");

// 发送验证码
const sendCode = () => {
  if (isNull(loginInfo.value.account)) {
    errorNotify("请输入手机号或者邮箱");
    return;
  }
  commonApi.sendCode(loginInfo.value.account!).then((res) => {
    if (res.data.code === 0) {
      successNotify(res.data.data);
    }
  });
};


// 登录
const login = () => {
  userApi.login(loginInfo.value).then((res) => {
    if (res.data.code === 0) {
      console.log(res.data.data);
      setToken(res.data.data);
      // 刷新页面
      window.location.reload();
    }
  });
};
</script>

<style lang="scss">
.login-box__ .login-info {
  height: 200px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-direction: column;
}

/* 样式穿透tabs标签 */
.el-tabs__header.is-top {
  /* 使tabs居中 */
  display: flex;
  justify-content: center;
}

.el-tabs__nav-wrap.is-top {
  /* 缩短tabs下划线 */
  width: 200px;
}

#tab-pwdLogin,
#tab-codeLogin {
  /* 设置tabs字体 */
  font-size: 20px;
  font-weight: 700;
}

.login-box__ .el-form {
  /* 固定form宽度 */
  width: 400px;
}
</style>
